package com.sudo.service.admin.mail;

import com.sudo.common.component.TemplateUtil;
import com.sudo.common.enums.SysJobAction;
import com.sudo.dao.admin.entity.SysJobLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;


/**
 * 构建邮件模板
 */
@Component
public class MailPojoUtil {

    // 任务调度日志邮件（如果失败，则发邮件提醒）
    public static MailPojo methodJobExceptionMail(String mailAddress, SysJobLog sysJobLog){
        MailPojo mailPojo = new MailPojo();
        String sysJobAction = SysJobAction.valueOf(sysJobLog.getJobAction()).value;
        String title = "任务【" + sysJobLog.getJobId() + "】["+ sysJobAction +"]运行失败：" + sysJobLog.getJobNote();
        mailPojo.setTitle(title);
        StringBuilder text = new StringBuilder();
        if (StringUtils.isNotBlank(sysJobLog.getJobServer())) {
            text.append("【节点】：").append(sysJobLog.getJobServer()).append("<br/>");
        }else {
            text.append("【节点】：本地服务").append("<br/>");
        }
        text.append("【任务】：").append(sysJobLog.getJobId()).append("<br/>");
        text.append("【模块】：").append( sysJobLog.getJobBean()).append("<br/>");
        text.append("【方法】：").append(sysJobLog.getJobMethod()).append("<br/>");
        if(StringUtils.isNotBlank(sysJobLog.getJobParams())){
            text.append("【参数】：").append(sysJobLog.getJobParams()).append("<br/>");
        }else {
            text.append("【参数】：无").append("<br/>");
        }
        text.append("【耗时】：").append(sysJobLog.getJobUseTime()).append("ms").append("<br/>");
        text.append("【运行时间】：").append(sysJobLog.getJobStartTime()).append("<br/>");
        text.append("【结束时间】：").append(sysJobLog.getJobEndTime()).append("<br/>");
        text.append("【异常说明】：").append(sysJobLog.getJobNote()).append("<br/>");
        text.append("【异常详情】：").append(sysJobLog.getJobNoteDesc()).append("<br/>");

        mailPojo.setText(text.toString());
        String[] mailSendArr = mailAddress.split(",");
        mailPojo.setSentTo(mailSendArr);
        return mailPojo;
    }

    // 用户注册，邮箱验证码
    public static MailPojo emailVerifyCode(String emailAddress, String code) {
        MailPojo mailPojo = new MailPojo();
        mailPojo.setTitle("JBuss验证码");
        mailPojo.setText("欢迎使用JBuss，您的验证码是：" + code);
        String[] mailSendArr = emailAddress.split(",");
        mailPojo.setSentTo(mailSendArr);
        return mailPojo;
    }

}
